//
//  DeveloperViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/9/14.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "DeveloperViewController.h"
#import "DeveloperTableViewCell.h"

@interface DeveloperViewController ()<UITableViewDelegate,UITableViewDataSource>{

    UITableView * _tableView;
    NSArray * _dataArray;
    int _page;

}

@end

@implementation DeveloperViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    TitleView(@"开发者");
    
    _page = 1;
    

    [self getActivityListData];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(back)];
    
    [self creatTableView];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[DeveloperTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[DeveloperTableViewCell reuseIdentifier]];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        [self getActivityListData];
        
        
    }];
    _tableView.mj_header = header;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 90;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    
    return 0.01;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    DeveloperTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[DeveloperTableViewCell reuseIdentifier] forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell.userImage sd_setImageWithURL:ImageUrl(dic[@"avatar"]) placeholderImage:ImageName(@"img_user_default")];
    cell.userName.text = [NSString stringWithFormat:@"%@",dic[@"user_name"]];
    cell.userAddress.text = [NSString stringWithFormat:@"%@",dic[@"area"]];
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
     NSDictionary * dic = _dataArray[indexPath.row];
     self.block(dic[@"user_name"],dic[@"developer_id"]);
     [self.navigationController popViewControllerAnimated:YES];

}

-(void)getActivityListData{
    
      NSDictionary * dic = @{
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"page_num":@"1000",
                           };
    
    //    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_getDeveloper_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            if ([responseObject[@"datas"][@"list"] isKindOfClass:[NSArray class]]) {
                
                _dataArray = responseObject[@"datas"][@"list"];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
                   
               }else {
                    _tableView.mj_footer.hidden = YES;
                }
                [_tableView.mj_header endRefreshing];
          
                [_tableView reloadData];
                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        [_tableView.mj_header endRefreshing];
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
}


@end
